package com.tyk.bookstore.front.storage.service.impl;


import com.tyk.bookstore.front.common.core.util.AssertUtil;
import com.tyk.bookstore.front.common.core.util.ObjUtil;
import com.tyk.bookstore.front.common.dal.mapper.dto.BinDto;
import com.tyk.bookstore.front.common.dal.mapper.dto.SinDto;
import com.tyk.bookstore.front.storage.model.dto.Warehouse4OrderDto;
import com.tyk.bookstore.front.storage.model.dto.Warehouse4OrderIdDto;
import com.tyk.bookstore.front.storage.model.entity.WarehouseDo;
import com.tyk.bookstore.front.storage.model.enume.WarehouseResultEnum;
import com.tyk.bookstore.front.storage.repository.WarehouseRepository;
import com.tyk.bookstore.front.storage.service.WarehouseApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tyk
 * @description 仓库对内暴露接口
 */
@Service
@RequiredArgsConstructor
public class WarehouseApiServiceImpl implements WarehouseApiService {
    private final WarehouseRepository warehouseRepository;


    @Override
    public List<Long> getIdList() {
        return this.warehouseRepository.selectIdList();
    }

    @Override
    public List<Warehouse4OrderIdDto> getAreaCodeListByIdList(List<Long> idList) {
        if (ObjUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        List<Warehouse4OrderIdDto> dtoList = this.warehouseRepository.selectAreaCodeListByIdList(idList);
        AssertUtil.equal(dtoList.size(), idList.size(), WarehouseResultEnum.WAREHOUSE_NOT_FOUND);
        return dtoList;
    }

    @Override
    public String getWarehouseNameById(Long id) {
        SinDto<String> dto = warehouseRepository.selectSin1Eq(
                WarehouseDo::getName,
                WarehouseDo::getId, id,
                WarehouseDo.class
        );
        AssertUtil.notNull(dto, WarehouseResultEnum.WAREHOUSE_NOT_FOUND);
        return dto.getV1();
    }

    @Override
    public Warehouse4OrderDto getWarehouse4OrderDtoById(Long id) {
        BinDto<String, String> dto = warehouseRepository.selectBin1Eq(
                WarehouseDo::getName, WarehouseDo::getSenderName,
                WarehouseDo::getId, id,
                WarehouseDo.class
        );
        AssertUtil.notNull(dto, WarehouseResultEnum.WAREHOUSE_NOT_FOUND);
        return new Warehouse4OrderDto(dto.getV1(), dto.getV2());
    }
}
